package com.rainng.coursesystem.service.member;

import com.rainng.coursesystem.manager.OptionManager;
import com.rainng.coursesystem.manager.member.EventManager;
import com.rainng.coursesystem.model.entity.MemberEventEntity;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.service.BaseService;
import org.springframework.stereotype.Service;

@Service("member_eventService")
public class EventService extends BaseService {
    private final EventManager manager;
    private final OptionManager optionManager;

    public EventService(EventManager manager, OptionManager optionManager) {
        this.manager = manager;
        this.optionManager = optionManager;
    }

    public ResultVO list() {
        Integer memberId = getUserId();
        return result(manager.listMemberEventSelected(memberId));
    }

    public ResultVO delete(Integer memberEventId) {
        Integer memberId = getUserId();
        if (!optionManager.getAllowMemberSelect()) {
            return failedResult("现在不是选项目退项目时间!");
        }
        MemberEventEntity memberEvent = manager.getMemberEventById(memberEventId);
        if (memberEvent == null) {
            return failedResult("团员选项目Id:" + memberEventId + "不存在");
        }
        if (!memberEvent.getMemberId().equals(memberId)) {
            return failedResult("此项目非此团员所选!");
        }
        if (memberEvent.getBidPrice() != null || memberEvent.getShouldPrice() != null || memberEvent.getBudget() != null) {
            return failedResult("团员已选项目, 不能退选");
        }

        manager.deleteMemberEvent(memberEvent);
        return result("退选成功");
    }
}
